﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using coffeeshop.coffeeDataSetTableAdapters;
using System.Data;

namespace coffeeshop.Models
{
    public class ProductIngredient
    {
        private static ProductIngredientTableAdapter _adapter = new ProductIngredientTableAdapter();

        public int ProductIngredientId { get; set; }
        public int CompoundProductId { get; set; }
        public int ProductId { get; set; }
        public int ForProductId { get; set; }
        public float Quantity { get; set; }
        public string UnitName { get; set; }

        public bool Save()
        {
            try
            {
                if (ProductIngredientId == 0)
                {
                    coffeeDataSet.ProductIngredientDataTable data = _adapter.AddIngredient(CompoundProductId, ProductId, ForProductId, Quantity);
                    ProductIngredientId = (int)data.Rows[0]["ProductIngredientId"];
                }
                else
                {
                    
                }

                return true;
            }
            catch
            {
                return false;
            }
        }

        public static List<ProductIngredient> GetProductIngredients(int productId)
        {
            coffeeDataSet.ProductIngredientDataTable data = _adapter.GetIngredientListForProductId(productId);

            List<ProductIngredient> _list = new List<ProductIngredient>();
            ProductIngredient pi;
            foreach (DataRow row in data.Rows)
            {
                pi = new ProductIngredient();
                pi.ProductIngredientId = (int)row["ProductIngredientId"];
                pi.CompoundProductId = (int)row["CompoundProductId"];
                pi.ForProductId = (int)row["ForProductId"];
                pi.ProductId = (int)row["ProductId"];
                pi.Quantity = Convert.ToSingle(row["Quantity"]);
                pi.UnitName = (string)row["UnitName"];
                _list.Add(pi);
            }
            return _list;
        }
    }
}
